exports.ElevatorUI = Animation.AASprite.subclass(//GL2.Sprite.subclass(
{
    classname: 'ElevatorUI',
    
    initialize: function($super, model, pickSide) 
    {
    	$super();
    	
    	this._modelObj = null;
    	this._pickSide = kSide.kSideNone;
    	this._currentFloor = 0;
    	this._destinationFloor = 0;
    	this._destinationFloorsArray = null;
    	this._avatarUIArray = null;
    	this._elevatorDirection = null;
    	this._isMoving = false;
    	this._itemModel = null;
    	this.touchCountEnd = 0;
    	this.touchCountStart = 0;
    	this._upgradeIndicatorImage = null;
    	this._extractFloorAndMoveTimeout = null;
    	
    	this._modelObj = model;
    	this._currentFloor = 0;
    	this._destinationFloor = 1;
    	this._elevatorMovement = kElevatorMovement.kElevatorMovementStill;
    	this._avatarUIArray = new GAMutableArray();
    	this._destinationFloorsArray = new GAMutableArray();
    	this._itemModel = model.getItemModel();
    	
    	if(pickSide)
    	{
    		this._pickSide = pickSide;
    	}
    	
    	for(var i=0 ; i<MMGameLayer.getMallUI().getMallObj().getFloorCount() ; i++)
    	{
    		this.addDestinationFloorRow();
    	}
    },
	
	createView: function()
	{	
		this.setRelevantImageTimeout();
		this.setPosition(this.getAdjustedPositionForSide(this._pickSide));
		this.handleTouches();
		this.setScaleForSide(this._pickSide);
		this.checkForUpgrade();
	},
	
	showTutorialForUpgrade: function()
	{
		this._tutorialIndicatorForUpgrade = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForUpgrade.setPosition([TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(370)]);
		this._tutorialIndicatorForUpgrade.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForUpgrade);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_8"));	
	},
	
	showTutorialForInstantUpgrade: function()
	{
		this._tutorialIndicatorForInstantUpgrade = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForInstantUpgrade.setPosition([TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(370)]);
		this._tutorialIndicatorForInstantUpgrade.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForInstantUpgrade);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(160)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_9"));
	},
	
	checkForUpgrade: function()
	{
		var elevatorModel = this.getModelObject(); 
		if(elevatorModel.isUpgradeInProgress())
		{
			var upgradeRemainingTime = elevatorModel.upgradeRemainingTime();
			if(upgradeRemainingTime > 0)
			{
				this.setUpgradeImages();
				this._upgradeTimeout = setTimeout(this.upgradeDoneCallBack.bind(this), upgradeRemainingTime*1000);
			}
			else
			{
				this.upgradeDoneCallBack();
			}
		}
	},
	
	getAdjustedPositionForSide: function(pickSide)
	{
		if(pickSide == kSide.kSideLeft)
		{
			return [TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(-5)];			
		} 
		else if(pickSide == kSide.kSideRight)
		{
			return [TCUtil.getAdjustedX(318), TCUtil.getAdjustedY(-5)];
		} 
	},
	
	getRelevantImagePath: function()
	{
		var path = this.getBaseResourcePath();
		if(this._modelObj.isInBreakdownMode())
		{
			if(this._upgradeIndicatorImage)
			{
				this._upgradeIndicatorImage.destroy();
				this._upgradeIndicatorImage = null;
			}
			path = path + ""+this._modelObj.getUpgradeState()+"_0_broken.png";
		}
		else if (this._modelObj.isUpgradeInProgress())
		{
			if(!this._upgradeIndicatorImage)
			{
				var upgradeIndicatorImagePath = "./Content/images/gamelayer/environmentlayer/exclamation.png";
				
				this._upgradeIndicatorImage = GLFactory.getSprite([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0)], 10, upgradeIndicatorImagePath, [TCUtil.getAdjustedX(30),TCUtil.getAdjustedY(60)],[0,1]);
				this.addChild(this._upgradeIndicatorImage);
			}
			path = path + ""+this._modelObj.getUpgradeState()+"_0.png";
		}
		else 
		{
			if(this._upgradeIndicatorImage)
			{
				this._upgradeIndicatorImage.destroy();
				this._upgradeIndicatorImage = null;
			}
			path = path + ""+this._modelObj.getUpgradeState()+"_0.png";
		}
		
		return path;
	},
	
	setRelevantImage: function()
	{
		var elevatorImagePath = this.getRelevantImagePath();
		this.setImage(elevatorImagePath, [TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(60)], [0,1]);
	},
	
	setRelevantImageTimeout: function()
	{
//		if(!this._modelObj.isInBreakdownMode())
//		{
//			this.setRelevantImageTimeout = setTimeout(this.setRelevantImage.bind(this), this._modelObj.getRemainingBreakdownTime()*1000);
//		}
		this.setRelevantImage();
	},
	
	setScaleForSide: function(pickSide)
	{
		if(pickSide == kSide.kSideLeft)
		{
			this.setScale(new Core.Vector(1,1));			
		} 
		else if(pickSide == kSide.kSideRight)
		{
			this.setScale(new Core.Vector(-1,1));
		} 	
	},
	
	addDestinationFloorRow: function()
	{
		if(this._destinationFloorsArray)
		{
			this._destinationFloorsArray.addObject(0);
		}
	},
	
	extractFloorAndMove: function()
	{
		if(this._destinationFloorsArray)
		{
			var destinationFloor = this.getDestinationFloorForMovement(this._elevatorMovement);
			if(destinationFloor != -1)
			{
				this._elevatorMovement = this.getMovementForDestination(destinationFloor);
				this.moveToFloor(destinationFloor);
			}
			else
			{
				if(this._elevatorMovement == kElevatorMovement.kElevatorMovementUp)
				{
					destinationFloor = this.getDestinationFloorForMovement(kElevatorMovement.kElevatorMovementDown);
				}
				else if(this._elevatorMovement == kElevatorMovement.kElevatorMovementDown)
				{
					destinationFloor = this.getDestinationFloorForMovement(kElevatorMovement.kElevatorMovementUp);
				}
				if(destinationFloor != -1)
				{
					this._elevatorMovement = this.getMovementForDestination(destinationFloor);
					this.moveToFloor(destinationFloor);
				}
				else
				{
					this._elevatorMovement = kElevatorMovement.kElevatorMovementStill;
				}
			}
		}
	},
	
	getDestinationFloorForMovement: function(movement)
	{
		if(movement == kElevatorMovement.kElevatorMovementStill)
		{
			for(var i=this._currentFloor ; i<this._destinationFloorsArray.getLength() ; i++)
			{
				var value = this._destinationFloorsArray.objectAtIndex(i);
				if(value != 0)
				{
					return i;
				}
			}
			for(var i=this._currentFloor ; i>=0 ; i--)
			{
				var value = this._destinationFloorsArray.objectAtIndex(i);
				if(value != 0)
				{
					return i;
				}
			}
		}
		else if(movement == kElevatorMovement.kElevatorMovementUp)
		{
			for(var i=this._currentFloor ; i<this._destinationFloorsArray.getLength() ; i++)
			{
				var value = this._destinationFloorsArray.objectAtIndex(i);
				if(value != 0)
				{
					return i;
				}
			}
			
		}
		else if(movement == kElevatorMovement.kElevatorMovementDown)
		{
			for(var i=this._currentFloor ; i>=0 ; i--)
			{
				var value = this._destinationFloorsArray.objectAtIndex(i);
				if(value != 0)
				{
					return i;
				}
			}
			
		}
		return -1;
	},
	
	addDestinationFloor: function(floorId)
	{
		if(this._destinationFloorsArray && floorId < this._destinationFloorsArray.getLength())
    	{
    		this._destinationFloorsArray.replaceObjectAtindex(floorId, 1);
    		
    		if(this._elevatorMovement == kElevatorMovement.kElevatorMovementStill)
    		{
    			this._extractFloorAndMoveTimeout = setTimeout(this.extractFloorAndMove.bind(this), 1000);
    		}
    	}
	},
	
	addAvatarToElevator: function(avatarUI)
	{
		if(avatarUI && this._avatarUIArray)
		{
			this._avatarUIArray.addObject(avatarUI);
		}
	},
	
	getAvatarsUIArray: function()
	{
		if(this._avatarUIArray)
		{
			return this._avatarUIArray;
		}
	},
	
	canAvatarBeAddedForDestination: function(destination)
	{
//		if(this.getModelObject().isUpgradeInProgress())// to stop elevator while upgrading uncomment
//		{
//			return false;
//		}
		var targetMovement = this.getMovementForDestination(destination);
		if(this._elevatorMovement == targetMovement || this._elevatorMovement == kElevatorMovement.kElevatorMovementStill)
		{
			return true;
		}
		return false;
	},
	
	isElevatorFull: function()
	{
		return false;
	},
	
	getMovementForDestination: function(floorId)
	{
		if(this._currentFloor < floorId)
		{
			return kElevatorMovement.kElevatorMovementUp;
		} 
		else if(this._currentFloor > floorId)
		{
			return kElevatorMovement.kElevatorMovementDown;
		}
		else 
		{
			return kElevatorMovement.kElevatorMovementStill;
		}
	},
	
	moveToFloor: function(floor)
	{
		this._destinationFloor = floor;
			
		var totalFloors = MMGameLayer.getMallUI()._floorsUIArray.getLength();
		if(this._destinationFloor == this._currentFloor)
		{
			this.goToNextFloorSuccessCB();
		}
		else if(this._destinationFloor < totalFloors && this._destinationFloor >= 0)
		{
			this.goToNextFloor(this._destinationFloor);
		}
	},
	
	goToNextFloor: function(nextFloor)
	{
		this._isMoving = true;
		
		var speed = this._modelObj.getSpeed();
		var y = TCUtil.getAdjustedY(-5 - (FLOOR_HEIGHT * (nextFloor)));
		var x = this.getPosition().getX();
		
		var distanceToMove = Math.abs(this.getPosition().getY() - y);
		var time = distanceToMove/speed;
		
		var move2 = new Animation.AAMoveTo(new Core.Point(x, y), time);
		var moveToFloor = new Animation.AACallFuncND(this.goToNextFloorSuccessCB.bind(this), null); 
		
		var sequence = new Animation.AASequence(move2, moveToFloor);
		this.runAction(sequence);		
	},
	
	goToNextFloorSuccessCB: function()
	{
		if(this._destinationFloorsArray)
		{
			this._currentFloor = this._destinationFloor;
			this._isMoving = false; // DO NOT CHANGE ITS DAMN POSITION
			this._destinationFloorsArray.replaceObjectAtindex(this._destinationFloor, 0);
			
			ElevatorHandler.checkInsAndOutsForElevator(this);
			this._extractFloorAndMoveTimeout = setTimeout(this.extractFloorAndMove.bind(this), 1000);
		}		
	},
	
	getIsMoving: function()
	{
		return this._isMoving;
	},
	
	getPickSide: function()
	{
		return this._pickSide;
	},
	
	getElevatorMovement: function()
	{
		return this._elevatorMovement;
	},
	
	getCurrentFloor: function()
	{
		return this._currentFloor;
	},
	
	setCurrnetFloor: function(currentFloor)
	{
		this._currentFloor = currentFloor;
	},
	
	handleTouches: function() 
	{
		this._touchTarget = new GL2.TouchTarget();
		this._touchTarget.setSize(TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(60));
		this._touchTarget.setAnchor(0, 1);
		this._touchTarget.setPosition(0, 0);
		this._touchTarget.getTouchEmitter().addListener(MMGameLayer.getMallContainer().getMallUI()._touchHandler, this.onTouch.bind(this),5);
		this._touchTarget.setDepth(100000);
		this.addChild(this._touchTarget);
		
	},
	
	upgradeElevator: function()
	{
		var elevatorModel = this.getModelObject();
		if(!elevatorModel.canUpgradeElevator())
    	{
    		return;
    	} 
    	var pricing = elevatorModel.getUpgradePricing();
		if(elevatorModel.canUpgradeBePurchased(pricing.currency, pricing.cost))
		{
			MMSoundManager.playEffect(NEW_AND_UPGRADE_SOUND);
			elevatorModel.upgradeElevator();
			this.setUpgradeImages();
			var upgradeRemainingTime = elevatorModel.upgradeRemainingTime();
			this._upgradeTimeout = setTimeout(this.upgradeDoneCallBack.bind(this), upgradeRemainingTime*1000);
			if(pricing.currency == kCurrencyType.kCurrencyTypeCoins)
			{
				AnalyticsHandler.logAnalyticsOffer("Upgrade", this._modelObj.getUpgradePricing() , "Coins",this._modelObj.getItemId());
			}
			else
			{
				AnalyticsHandler.logAnalyticsOffer("Upgrade", this._modelObj.getUpgradePricing() , "Nuggets",this._modelObj.getItemId());
			}
		}
		else
		{
			var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("NOT_ENOUGH_MONEY"), ["ok"], this);
			//TODO Put Alert something to show user that he/she dont have enough currency to do action
		}
		if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 8)
    		{
    			this._tutorialIndicatorForUpgrade.destroy();
    			this._tutorialIndicatorForUpgrade = null;
				TutorialManager.showNextTutorialStep();
    		}
    	}
	},
	
	setUpgradeImages: function()
	{
		this.setRelevantImage();
	},
	
	upgradeDoneCallBack: function()
	{
    	if(!TutorialManager.getHasCompletedTutorial())
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 9)
    		{
    			if(this._tutorialIndicatorForInstantUpgrade)
    			{
	    			this._tutorialIndicatorForInstantUpgrade.destroy();
	    			this._tutorialIndicatorForInstantUpgrade = null;	
    			}
    			TutorialManager.showNextTutorialStep();
    		}	
    	}
		this.getModelObject().upgradeCompleted();
		this.setRelevantImage();
		
		ElevatorHandler.checkAndAssignElevator();
	},
	
	getModelObject: function()
	{
		return this._modelObj;
	},
	
	getThumbImagePath: function()
	{
		var tempPath = this._modelObj.getItemId().split("_")
		return "./Content/images/storeimages/" + tempPath[0] + "/" + tempPath[1] + "/" + tempPath[2] + "/" +"default/thumb.png";
	},
	
	onTouch: function(touch)
	{
		MMGameLayer.getMallUI().getTouchHandler().onTouch(touch);
		switch(touch.getAction())
		{
			case touch.Action.Start:
				this._touchStart();
				return true;
			break;
            
            case touch.Action.End:
			break;
            
            case touch.Action.Move:
			break;
		}		
	},
	
	_touchStart: function()
	{
		if(!DatabaseInterface._isVisitingNeighbor)
		{
			if(TutorialManager.getHasCompletedTutorial())
			{
				var attributeWindow = new MMElevatorAttributeWindow(this);
				attributeWindow.createView();
				MMGameLayer.getControlLayer().addChild(attributeWindow);
			}
			else
			{
				if(TutorialManager.getCurrentTutorialStep() == 8 || TutorialManager.getCurrentTutorialStep() == 9)
				{
					var attributeWindow = new MMElevatorAttributeWindow(this);
					attributeWindow.createView();
					MMGameLayer.getControlLayer().addChild(attributeWindow);
				}
			}
		}	
	},
	
	getBaseResourcePath: function()
	{
		var tempPath = this._modelObj.getItemId().split("_")
		
		return "./Content/images/storeimages/" + tempPath[0] + "/" + tempPath[1] + "/" + tempPath[2] + "/" +"default/";
	},
								   
	destroy: function()
    {
    	this.stopAllActions();
    	this._modelObj = null;
    	if(this._upgradeIndicatorImage)
    	{
    		this._upgradeIndicatorImage.destroy();
    		this._upgradeIndicatorImage = null;
    	}
		if(this._destinationFloorsArray)
		{
			this._destinationFloorsArray.destroy();
			this._destinationFloorsArray = null;
		}
		if(this._upgradeTimeout)
		{
			clearTimeout(this._upgradeTimeout);
			this._upgradeTimeout = null;
		}
		if(this._extractFloorAndMoveTimeout)
		{
			clearTimeout(this._extractFloorAndMoveTimeout);
			this._extractFloorAndMoveTimeout = null;
		}
		
		if(this._avatarUIArray)
		{
			this._avatarUIArray.destroy();
			this._avatarUIArray = null;
		}
    },
});
